package com.shuai.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shuai.stickynote.MemoryNote;
import com.shuai.stickynote.R;

import java.util.ArrayList;

public class MemoryListChildAdapter extends BaseAdapter {
    private ArrayList<MemoryNote> data;
    private Context context;

    public MemoryListChildAdapter(Context context) {
        this.context = context;
    }

    public void replaceAll(ArrayList<MemoryNote> memorys){
        if (data == null) data = new ArrayList<>();
        data.clear();
        if (memorys != null && memorys.size() > 5) {
            for (int i = 0; i < 5; i++) {
                data.add(memorys.get(i));
            }
        }else if (memorys != null){
            data.addAll(memorys);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.adapter_memory_list_child_item,null);
            holder = new ViewHolder();
            holder.tv_content = convertView.findViewById(R.id.adapter_memory_list_child_content);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position < 5)
            holder.tv_content.setText(data.get(position).getEn()+"\t\t"+data.get(position).getCh());
        return convertView;
    }

    static class ViewHolder{
        TextView tv_content;
    }
}
